<?php
namespace Apps\Common\Libs;
error_reporting(0);
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use JPush\Exception\APIConnectionException;
use JPush\Exception\APIRequestException;
use PhalconPlus\Common\Component;
use PhalconPlus\Common\Util;

class SmaUtil extends Component
{
    private $token = "thebossawageincrease";
    private $url;

    /**
     * 获取用户身份
     * @date: 2016年3月10日
     * @author: 545
     */
    public function getIdentity($mobile){
        
        $this->url  = $this->getDi()->get('appConfig')->burl->getboss;
    
        $timestamp = time();
        $nonce =  Util::RandStr(12);
        $sign =  $this->getSign($this->token, $timestamp, $nonce) ;
        $params['timestamp'] = $timestamp;
        $params['nonce'] = $nonce;
        $params['sign'] = $sign;
        $params['mobile'] = $mobile;
    
        try {
            $dataJson = Util::HttpPost($this->url, $params);
        }
        catch (\Exception $e){
            return false;
        }
    
        $data = json_decode($dataJson,true);
    
        if ($data['status']==10000){
            return $data['result'];
        }
    
        return  false;
    }
    
    /**
     * 获取分店列表
     * @date: 2016年3月10日
     * @author: 545
     */
    public function getBranch($mobile){
    
        $this->url = $this->getDi()->get('appConfig')->burl->getboss;
    
        $timestamp = time();
        $nonce =  Util::RandStr(12);
        $sign =  $this->getSign($this->token, $timestamp, $nonce) ;
        $params['timestamp'] = $timestamp;
        $params['nonce'] = $nonce;
        $params['sign'] = $sign;
        $params['type'] = 1;
        $params['mobile'] = $mobile;
    
        try {
            $dataJson = Util::HttpPost($this->url, $params);
        }
        catch (\Exception $e){
            return false;
        }
        $data = json_decode($dataJson,true);
        if ($data['status']==10000){
            return $data['result'];
        }
    
        return  false;
    }
    
    /**
     * 更新SMA端状态
     * @date: 2016年3月19日 
     * @author: 545
     */
    public function updateOnline($shopid){
        
        $this->url = $this->getDI()->get('appConfig')->burl->updateonline;
        
        $timestamp = time();
        $nonce =  Util::RandStr(12);
        $sign =  $this->getSign($this->token, $timestamp, $nonce) ;
        $params['timestamp'] = $timestamp;
        $params['nonce'] = $nonce;
        $params['sign'] = $sign;
        $params['shop_id'] = $shopid;
        
        try {
            $dataJson = Util::HttpPost($this->url, $params);
        }
        catch (\Exception $e){
            return false;
        }
        
        $data = json_decode($dataJson,true);
        
        if ($data['status']==10000){
            return $data['result'];
        }
        
        return  false;
    }
    
    /**
     * 根据分店id获取 手机号
     * @date: 2016年3月22日 
     * @author: 545
     */
    public function getMobile($branchid){
        $this->url = $this->getDi()->get('appConfig')->burl->getmobile;
        
        $timestamp = time();
        $nonce =  Util::RandStr(12);
        $sign =  $this->getSign($this->token, $timestamp, $nonce) ;
        $params['timestamp'] = $timestamp;
        $params['nonce'] = $nonce;
        $params['sign'] = $sign;
        $params['shop_id'] = $branchid;
        
        try {
            $dataJson = Util::HttpPost($this->url, $params);
        }
        catch (\Exception $e){
            return false;
        }
        
        $data = json_decode($dataJson,true);
        if ($data['status']==10000){
            return $data['result'];
        }
        
        return  false;
    }
    
    /**
     * 获取统计数据
     * @date: 2016年3月10日
     * @author: 545
     */
    public function getStatistics($branchid){
    
        $this->url = $this->getDi()->get('appConfig')->burl->statistics;
    
        $timestamp = time();
        $nonce =  Util::RandStr(12);
        $sign =  $this->getSign($this->token, $timestamp, $nonce) ;
        $params['timestamp'] = $timestamp;
        $params['nonce'] = $nonce;
        $params['sign'] = $sign;
        $params['type'] = 1;
        $params['shop_id'] = $branchid;
    
        try {
            $dataJson = Util::HttpPost($this->url, $params);
        }
        catch (\Exception $e){
            return false;
        }
        
        $data = json_decode($dataJson,true);
    
        if ($data['status']==10000){
            return $data['result'];
        }
    
        return  false;
    }
    
    /**
     * 添加预约服务|美容院
     * @date: 2016年3月10日
     * @author: 545
     */
    public function setServiceOrder($branchid,$order_time,$order_member_name,$order_member_phone,$order_item){
    
        $this->url = $this->getDi()->get('appConfig')->burl->serviceorder;
    
        $timestamp = time();
        $nonce =  Util::RandStr(12);
        $sign =  $this->getSign($this->token, $timestamp, $nonce) ;
        $params['timestamp'] = $timestamp;
        $params['nonce'] = $nonce;
        $params['sign'] = $sign;
        $params['type'] = 1;
        $params['shop_id'] = $branchid;
        $params['order_time'] = $order_time;
        $params['order_member_name'] = $order_member_name;
        $params['order_member_phone'] = $order_member_phone;
        $params['order_item'] = $order_item;
    
        try {
            $dataJson = Util::HttpPost($this->url, $params);
        }
        catch (\Exception $e){
            return false;
        }
    
        $data = json_decode($dataJson,true);
    
        if ($data['status']==10000){
            return $data['result'];
        }
    
        return  false;
    }
    
    /**
     * 生成签名
     * @date: 2016年2月24日
     * @author: 545
     */
    private function getSign($token,$timestamp,$once){
        $signatureArray = array($token, $timestamp, $once);
        return sha1(implode($signatureArray));
    }
    
}
